package com.guguju.ssms.core.http;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.guguju.ssms.Guguju;
import com.lzy.okgo.model.HttpHeaders;

/**
 * Created by dell on 2022-5-14.
 */
public class HttpCommonHeader extends HttpHeaders {

    private static final long serialVersionUID = 9104417383747536756L;
    /**
     * context
     */
    private Application mContext;

    /**
     * 设备信息
     */
    private String agent;

    /**
     * 安装渠道(string)
     */
    private String setupChannel;

    /**
     * 应用版本号(string)
     */
    private String version;


    private String platfrom;

    /**
     * 设配机型 (string)
     */
    private String equipmentModel;

    /**
     * 手机系统版本(string)
     */
    private String equipmentOsVersion;

    /**
     * 安卓ID（string）
     */
    private String androidId;


    public HttpCommonHeader(@NonNull Application application) {
        mContext = application;
        put("User-Agent", getAgent());
        put("Content-Type", "application/json, text/json; charset=UTF-8");
        put("setupChannel", Guguju.setupChannel);
        put("version", getVersion());
        put("platfrom", "Android");
        put("equipmentModel", getEquipmentModel());
        put("equipmentOsVersion", getEquipmentOsVersion());
        put("androidId", getAndroidId());
    }

    /**
     * 获取设备信息
     *
     * @return
     */
    public String getAgent() {
        agent = "Guguju" + getVersion() + "" + getEquipmentModel();
        return agent;
    }


    /**
     * 获取设备机型
     *
     * @return
     */
    public String getEquipmentModel() {
        // 如果为真则 获取设备机型 getequipmentModel 中model为Debug Device
        // 响应的User-Agent:M6go 2.1.1 (Debug Device)| equipmentModel:Debug Device
        if (!TextUtils.isEmpty(Build.MODEL)) {
            equipmentModel = Build.MODEL;
        } else {
            // 如果没有获取到机型信息则默认给"GT-I9168"
            equipmentModel = "GT-I9168";
        }
        return equipmentModel;
    }

    /**
     * 获得应用版本号
     *
     * @return
     */
    private String getVersion() {
        try {
            PackageManager manager = mContext.getPackageManager();
            PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);
            version = info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
            version = "";
        }
        return version;
    }


    /**
     * 获取手机系统版本
     *
     * @return
     */
    public String getEquipmentOsVersion() {
        String versionInfo = android.os.Build.VERSION.RELEASE;
        if (!TextUtils.isEmpty(versionInfo)) {
            equipmentOsVersion = versionInfo;
        } else {
            equipmentOsVersion = "4.0.3";
        }
        return equipmentOsVersion;
    }

    /**
     * 获取android设备号
     *
     * @return
     */
    public String getAndroidId() {
        androidId = Settings.System.getString(mContext.getContentResolver(), Settings.System.ANDROID_ID);
        return androidId;
    }

    /**
     * 获取网络类型
     *
     * @return
     */
    public static int getNetTranslateType(Context context) {
        int netTranslateType = 1;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
        if (activeNetwork != null) { // connected to the internet
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                // connected to wifi
                netTranslateType = 1;
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                // connected to the mobile provider's data plan
                netTranslateType = 2;
            }
        } else {
            // not connected to the internet
            netTranslateType = 0;
        }
        return netTranslateType;
    }
}
